package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
import android.text.Spanned;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.lang.Character.UnicodeBlock;
import java.util.Arrays;

public class MainActivity2 extends AppCompatActivity {

    private EditText mEdit1;
    private EditText mEdit2;
    private EditText mEdit3;
    private EditText mEdit4;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity2.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main2 );
        initView();
        initViewAction();
    }

    private void initView() {

        mEdit1 = (EditText) findViewById( R.id.edit1 );
        mEdit2 = (EditText) findViewById( R.id.edit2 );
        mEdit3 = (EditText) findViewById( R.id.edit3 );
        mEdit4 = (EditText) findViewById( R.id.edit4 );
    }

    private void initViewAction() {

        EditInputFactory.setMaxLength( mEdit1, 8 );

        EditInputFactory.setMaxLength( mEdit2, 6 );
        EditInputFactory.chineseInputFilter( mEdit2 );

        EditInputFactory.setInputMobile( mEdit3 );
        EditInputFactory.setMaxLength( mEdit3, 11 );

        EditInputFactory.setInputSfz( mEdit4 );
    }

    public static class EditInputFactory {

        public static void setInputSfz( EditText editText ) {

            setMaxLength( editText, 18 );

            final String test = "0123456789Xx";
            InputFilter[] filters = editText.getFilters();
            InputFilter[] copy = Arrays.copyOf( filters, filters.length + 1 );
            copy[filters.length] = new InputFilter() {
                @Override
                public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {

                    StringBuilder builder = new StringBuilder();
                    for ( int i = start; i < end; i++ ) {
                        char c = source.charAt( i );
                        if ( test.contains( String.valueOf( c ) ) ) {
                            builder.append( c );
                        } else {
                            return "";
                        }
                    }
                    return builder.toString().trim();
                }
            };
            editText.setFilters( copy );

        }

        public static void setInputMobile( EditText editText ) {

            editText.setInputType( InputType.TYPE_CLASS_PHONE );
            setMaxLength( editText, 11 );
        }

        public static void setMaxLength( EditText editText, int maxLength ) {

            InputFilter[] filters = editText.getFilters();
            for ( int i = 0; i < filters.length; i++ ) {
                InputFilter filter = filters[i];
                if ( filter instanceof LengthFilter ) {
                    filters[i] = new LengthFilter( maxLength );
                    editText.setFilters( filters );
                    return;
                }
            }

            InputFilter[] copy = Arrays.copyOf( filters, filters.length + 1 );
            copy[filters.length] = new LengthFilter( maxLength );
            editText.setFilters( copy );
        }

        public static void chineseInputFilter( EditText editText ) {

            InputFilter[] filters = editText.getFilters();
            InputFilter[] copy = Arrays.copyOf( filters, filters.length + 1 );
            copy[filters.length] = new InputFilter() {
                @Override
                public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {

                    StringBuilder builder = new StringBuilder();
                    for ( int i = start; i < end; i++ ) {
                        char c = source.charAt( i );
                        if ( testHanZi( c ) ) {
                            builder.append( c );
                        } else {
                            return "";
                        }
                    }
                    return builder.toString().trim();
                }
            };
            editText.setFilters( copy );
        }

        public static boolean isChinese( char c ) {

            UnicodeBlock ub = UnicodeBlock.of( c );
            // 4E00-9FBF：CJK 统一表意符号
            return ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                //F900-FAFF：CJK 兼容象形文字
                || ub == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                //3400-4DBF：CJK 统一表意符号扩展 A
                || ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                //2000-206F：常用标点
                || ub == UnicodeBlock.GENERAL_PUNCTUATION
                //3000-303F：CJK 符号和标点
                || ub == UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                //FF00-FFEF：半角及全角形式
                || ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
        }

        public static boolean testHanZi( char c ) {

            if ( c == '·' ) {
                return true;
            }

            UnicodeBlock ub = UnicodeBlock.of( c );
            // 4E00-9FBF：CJK 统一表意符号
            return ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                //F900-FAFF：CJK 兼容象形文字
                || ub == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                //3400-4DBF：CJK 统一表意符号扩展 A
                || ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A;
        }

        /**
         * @param source 数据
         *
         * @return 是否中文名字
         */
        public static boolean testChinaName( CharSequence source ) {

            for ( int i = 0; i < source.length(); i++ ) {
                char c = source.charAt( i );
                if ( !testHanZi( c ) ) {
                    return false;
                }
            }
            return true;
        }
    }
}